Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added valid_password method in ValidationRules #227

Closed

Conversation

najdanovicivan
Copy link
Contributor

Added method in validation rules to be able to check user current password. This is intended to be used in admin form for checks when user is modifying password.

The rule takes one parameter which is user ID so it can be used in form/model validation like this

valid_password[{id}]

@michalsn
Copy link
Contributor

michalsn commented May 8, 2020

I think that we should separate the code responsible for validating password to a separate method in the LocalAuthenticator class (right now it's a part of validate() method).

Then we should use this method in validation rule since in the future valid_password implementation may depend on the used authentication library. So, I guess we shouldn't tightly couple this rule with LocalAuthenticator.

Also... if this rule is intended only for currently logged in users, why not skip the parameter part and use user() helper method to grab user data inside the rule?

@najdanovicivan
Copy link
Contributor Author

najdanovicivan commented May 8, 2020

@michalsn You're right about not needing parameter so I've updated the code to get the user via auth helper user() method.

I've also added one additional rule valid_password_with which works in the same manner as required_with so I had this rules previously. Which also was impacted by a CI4 issue codeigniter4/CodeIgniter4#2953

permit_empty|required_with[newPassword]|valid_password[{id}]

Which can now be replaced with single rule

valid_password_with[newPassword]

I'like the idea of using LocalAuthenticator but splitting the password check will also require updating the AuthenticatorInterface. So it might be a tricky to do this properly. The only way that looks feasible to me is moving password check to method called

validate_password($entity, $password)

And then use is_callable on Authenticator to check if validate_password exists as it does not make sense to have validate_password in the interface as it won't be possible to implement it in any password less authentication.

@michalsn
Copy link
Contributor

michalsn commented May 9, 2020

I've also added one additional rule valid_password_with which works in the same manner as required_with so I had this rules previously.

My personal preference is that if something can be done with simple 2 rules, then adding another one to do the same is unnecessary. Because we simply end up with an additional code that we have to maintain.

I'like the idea of using LocalAuthenticator but splitting the password check will also require updating the AuthenticatorInterface. So it might be a tricky to do this properly.

Actually I can imagine validate_password() method in the AuthenticatorInterface. If this method were not supported by the "driver", then I guess it would have to return false (or even throw an exception) - simple as that. There is still no official release yet, so I guess we can do this kind of change?

Maybe Lonnie will have some opinion about it and idea how to handle this. Or maybe he will say to just leave it as it is :)

@lonnieezell
Copy link
Owner

Sorry this one slipped by me. I agree with @michalsn here. I'm good adding that to the interface, but don't think valid_password_with is needed.

@lonnieezell
Copy link
Owner

@najdanovicivan Are you interested in making these changes or can I close?

@najdanovicivan
Copy link
Contributor Author

@lonnieezell I’m on it

@najdanovicivan najdanovicivan force-pushed the password-validator branch 2 times, most recently from 8e0ae51 to cd13444 Compare November 10, 2020 17:25
@najdanovicivan
Copy link
Contributor Author

najdanovicivan commented Nov 10, 2020

I've done the changes to include validate_password method in the AuthenticatorInterface but now I have some additional thoughts

What about renaming the method to validate_credential($user, $credential) that way it will be more generic so it will be useable for example with session tokens

Now in valid password $authenticator is get with Service methods which have $authenticationLib set to local as default.

It might be a good idea to change rule name to valid_credential as well and add an optional parameter for the authenticationLib with default value of local

@najdanovicivan
Copy link
Contributor Author

@lonnieezell @MGatner Can we see this one through so it can get merged. I did not get any response related to renaming the method to validate credential

@MGatner
Copy link
Collaborator

MGatner commented May 28, 2021

@lonnieezell will have to weigh in on the name change.

Copy link
Owner

@lonnieezell lonnieezell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking this over more during my review and I think this is a little too limited of a use-case to be included in the core library.

Thanks for your work on it, though, and hopefully you'll consider making other PR's in the future.

public function validate_password(User $user, string $password) : bool
{
// Can't validate without a password.
if (empty($credentials['password']) || count($credentials) < 2)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a few problems here:

  1. The $credentials array doesn't exist in this method
  2. I prefer to return early instead of wrapping code in an if statement like this.
  3. Even if all of the other stuff was good, the current implementation doesn't always return a boolean like the method signature states.

@@ -60,6 +60,27 @@ public function strong_password(string $value, string &$error1 = null, array $da
return $result;
}

/**
* A validation helper method to check if the passed
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* A validation helper method to check if the passed
* A validation helper method to check if the

*
* @return bool
*/
public function validate_password(User $user, string $password) : bool
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically all method names are camel-cased unless used for validation rules.

Suggested change
public function validate_password(User $user, string $password) : bool
public function validatePassword(User $user, string $password) : bool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants